#include <STC15F2K60S2.H>
#include "Delay.h"
#include "key.h"
#include "ds1302.h"
#include "display.h"
#include "time0.h"
#include "onewire.h"

sbit HC138_A = P2^5;	
sbit HC138_B = P2^6;
sbit HC138_C = P2^7;

sbit BUZZ = P0^6;
sbit RELAY = P0^4;

/*
0-9:数字0-9
10:'-'
11:空
12:"c"
*/
unsigned char code SMG_duanma[13]=
	{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
	0x80,0x90,0xbf,0xff,0xc6};


/*
function:选择锁存器
	4->LED
	7->数码管
	6->数码管公共端
*/
void Select_Mode(unsigned char position)
{
	switch(position)
	{
		case 4:	HC138_A = 0;HC138_B = 0;HC138_C = 1;break;
		case 5:	HC138_A = 1;HC138_B = 0;HC138_C = 1;break;
		case 6:	HC138_A = 0;HC138_B = 1;HC138_C = 1;break;
		case 7:	HC138_A = 1;HC138_B = 1;HC138_C = 1;break;
	}	
}

/*
function:亮灯
*/
void LED_ON(unsigned char position)
{
	Select_Mode(4);
	
	switch(position)
	{
		case 1:	P0 &= 0xfe;break;
		case 2:	P0 &= 0xfd;break;
		case 3:	P0 &= 0xfb;break;
		case 4:	P0 &= 0xf7;break;
		case 5:	P0 &= 0xef;break;
		case 6:	P0 &= 0xdf;break;
		case 7:	P0 &= 0xbf;break;
		case 8:	P0 &= 0x7f;break;
	}
		
}

/*
function:灭灯
*/
void LED_OFF(unsigned char position)
{
	Select_Mode(4);
	
	switch(position)
	{
		case 1:	P0 |= ~0xfe;break;
		case 2:	P0 |= ~0xfd;break;
		case 3:	P0 |= ~0xfb;break;
		case 4:	P0 |= ~0xf7;break;
		case 5:	P0 |= ~0xef;break;
		case 6:	P0 |= ~0xdf;break;
		case 7:	P0 |= ~0xbf;break;
		case 8:	P0 |= ~0x7f;break;
	}
		
}

void Sys_Init()
{
	Select_Mode(5);
	BUZZ = 0;
	RELAY = 0;
	P2 &= 0x1f;	//重置译码器，关闭锁存器
	
	
	Select_Mode(4);
	P0 |= 0xff; 
	P2 &= 0x1f;
	
}

unsigned char compass = 0;
unsigned char time_or_clock = 0;	//0:time
unsigned char clock_sec_low = 0, clock_sec_high = 0, clock_min_low = 0, clock_min_high = 0, clock_hour_low = 0, clock_hour_high = 0;
unsigned char clock_time[3] = {0, 0, 0};	//2:时	1;分	0:秒
unsigned char f_temp = 1;	//能否显示温度

extern unsigned char flag_LED_total, time_LED, t_LED, flag_LED_situ;

int main()
{	
	unsigned char Key_Value = 0;
	
	Sys_Init();
	Read_DS18B20_tem();	//滤除85
	Timer0_Init();	
	ds1302_write();
	while(1)
	{
		Key_Value = Independent_Key();
		
		ds1302_read();
		
		if(time[2] == clock_time[2] && time[1] == clock_time[1] && time[0] == clock_time[0])
		{
			flag_LED_total = 1;
			f_temp = 0;
			while(flag_LED_total == 1 && Key_Value == 0)
			{
				ds1302_read();
				Key_Value = Independent_Key();
			}
			LED_OFF(1);
			flag_LED_total = 0;
			time_LED = 0;
			t_LED = 0;
			flag_LED_situ = 0;
			f_temp = 1;
			Key_Value = 0;
		}
		
		if(Key_Value == 3)
		{
			f_temp = 0;
			time_or_clock = 0;
			compass = 1;
			while(compass != 7)
			{
				Key_Value = Independent_Key();
				
				switch(Key_Value)
				{
					case 1:		
						if(compass == 6){if(++sec_low == 10){sec_low = 9;}}
						if(compass == 5){if(++sec_high == 6){sec_high = 5;}}
						if(compass == 4){if(++min_low == 10){min_low = 9;}}
						if(compass == 3){if(++min_high == 6){min_high = 5;}}
						if(compass == 2){if(++hour_low == 4){hour_low = 3;}}
						if(compass == 1){if(++hour_high == 3){hour_high = 2;}}
						break;
							
					case 2:
						if(compass == 6){if(--sec_low > 10){sec_low = 0;}}
						if(compass == 5){if(--sec_high > 10){sec_high = 0;}}
						if(compass == 4){if(--min_low > 10){min_low = 0;}}
						if(compass == 3){if(--min_high > 10){min_high = 0;}}
						if(compass == 2){if(--hour_low > 10){hour_low = 0;}}
						if(compass == 1){if(--hour_high > 10){hour_high = 0;}}
						break;
						
					case 3:
						compass++;
						break;
				}
			}
			compass = 0;
			
			time[2] = hour_high * 10 + hour_low;
			time[1] = min_high * 10 + min_low;
			time[0] = sec_high * 10 + sec_low;
			ds1302_write();
		}
		else if(Key_Value == 4)
		{
			f_temp = 0;
			time_or_clock = 1;
			compass = 1;
			while(compass != 7)
			{
				Key_Value = Independent_Key();
				
				switch(Key_Value)
				{
					case 1:		
						if(compass == 6){if(++clock_sec_low == 10){clock_sec_low = 9;}}
						if(compass == 5){if(++clock_sec_high == 6){clock_sec_high = 5;}}
						if(compass == 4){if(++clock_min_low == 10){clock_min_low = 9;}}
						if(compass == 3){if(++clock_min_high == 6){clock_min_high = 5;}}
						if(compass == 2){if(++clock_hour_low == 4){clock_hour_low = 3;}}
						if(compass == 1){if(++clock_hour_high == 3){clock_hour_high = 2;}}
						break;
							
					case 2:
						if(compass == 6){if(--clock_sec_low > 10){clock_sec_low = 0;}}
						if(compass == 5){if(--clock_sec_high > 10){clock_sec_high = 0;}}
						if(compass == 4){if(--clock_min_low > 10){clock_min_low = 0;}}
						if(compass == 3){if(--clock_min_high > 10){clock_min_high = 0;}}
						if(compass == 2){if(--clock_hour_low > 10){clock_hour_low = 0;}}
						if(compass == 1){if(--clock_hour_high > 10){clock_hour_high = 0;}}
						break;
						
					case 3:
						compass++;
						break;
				}
			}
			time_or_clock = 0;
			compass = 0;
			
			clock_time[2] = clock_hour_high * 10 + clock_hour_low;
			clock_time[1] = clock_min_high * 10 + clock_min_low;
			clock_time[0] = clock_sec_high * 10 + clock_sec_low;
		}
	}
	
}
